package com.itheima.health.controller;

import com.itheima.health.common.PageParam;
import com.itheima.health.common.R;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.model.pojos.CheckGroup;
import com.itheima.health.service.JianchazuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class JianchazuController {
    @Autowired
    private JianchazuService jianchazuService;
   /*
   * 分页
   * */
@PostMapping("/checkgroup/findPage")
public ResultPageData fenye(@RequestBody PageParam pageParam){
    ResultPageData resultPageData = jianchazuService.fenye(pageParam);
    return resultPageData;
}

/*
* 新增
* */
    @PostMapping("/checkgroup/add")
    public R xinzeng(@RequestBody CheckGroup dto, @RequestParam("checkitemIds") String ids) {

            List<Integer> idList = Arrays.stream(ids.split(","))
                    .map(String::trim)
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());

            // 使用正确的参数类型调用服务层方法
            jianchazuService.xinzeng(dto, idList.toString());

            return R.success("新增成功");

    }

    /*
    * 回显
    * */
    @GetMapping("checkgroup/findById")
    public  R huixian(Integer id){
        CheckGroup checkGroup = jianchazuService.huixian(id);
        return  R.success(checkGroup);
    }

    /**
     * 修改
     */
    @PostMapping("checkgroup/edit")
    public R edit(@RequestParam("checkitemIds") String checkitemIdsStr,@RequestBody CheckGroup dto){
        Integer[] checkItemIds = null;
        if(checkitemIdsStr != "" || checkitemIdsStr.length() > 0){
            String[] split = checkitemIdsStr.split(",");
            checkItemIds = new Integer[split.length];
            for (int i = 0; i < split.length; i++) {
                checkItemIds[i] = Integer.parseInt(split[i].trim());
            }
        }
        jianchazuService.update(checkItemIds , dto);
        return R.success();
    }

    /*
    * 删除没有做
    * */

    /* 套餐新增
    */

 /*  @GetMapping("/checkgroup/findAll")
    public  R  xinzeng1(){
       jianchazuService.xinzeng1();
       return  R.success();
   }*/

}

